﻿using Ninject;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Wicresoft.EKA.MVCExtensions;
using Wicresoft.EKA.ITSM.Business;
using Wicresoft.EKA.ITSM.BusinessImpl;

namespace Wicresoft.EKA.ITSM.Web.WebPortal.Controllers
{
    public class PermissionAPIController : BaseAPIController
    {
        [Inject]
        public ISMGXJGroupAsso SMGXJGroupAsso { get; set; }

        [Inject]
        public ICTMPeople CTMPeople { get; set; }

        [Inject]
        public override IUserInfo UserInfo { get; set; }  //每个Controller必须包含的。

        protected override void Init()
        {
            ((IBase)SMGXJGroupAsso).UserIdentify = UserName;
            ((IBase)CTMPeople).UserIdentify = UserName;
            base.Init();
        }

        protected override void Dispose(bool disposing)
        {
            ((IBase)SMGXJGroupAsso).Dispose();
            ((IBase)CTMPeople).Dispose();
            base.Dispose(disposing);
        }

        public List<SMGXJGroupAssoModel> GetList(string LoginName)
        {
            Init();
            string Qual = "'LoginID' = \"" +LoginName+"\" AND 'Disabled' = \"No\"";
            return SMGXJGroupAsso.GetList(Qual);
        }

        public List<SMGXJGroupAssoModel> GetTemplateList()
        {
            Init();
            string Qual = "'LoginID' = \"Demo\" AND 'Short Description' = \"Template\" AND 'Disabled' = \"No\"";
            return SMGXJGroupAsso.GetList(Qual);
        }

        public object Post(List<SMGXJGroupAssoModel> list)
        {
            try
            {
                Init();
                string name = list.Select(o => o.LoginID).FirstOrDefault().ToString();
                SMGXJGroupAsso.Modify(name);
                SMGXJGroupAsso.Create(list);
                CacheHelper.ClearCache();
                return "Success";
            }
            catch (Exception e)
            {
                return "Fail: " + e.Message;
            }
        }

        public string GetFullName(string loginID)
        {
            try
            {
                Init();
                return CTMPeople.GetPeopleByLoginID(loginID).FullName;
            }
            catch (Exception e)
            {
                return "Fail: " + e.Message;
            }
        }

    }
}
